/**
 * 研发项目模块控制器
 */
const BaseController = require('./base_controller.js');
const ResearchService = require('../service/research_service.js');
const timeHelper = require('../config/project_helpers_config').timeHelper;
const ResearchStateBiz = require('../config/path_resolver.js').pageHelper;

class ResearchProjectController extends BaseController {

	/**
	 * 构造函数
	 */
	constructor() {
		super();
	}

	/**
	 * 项目列表页 [接口: research_project/list]
	 */
	async getProjectList() {
		try {
			// 数据校验
			let rules = {
				search: 'string|min:1|max:30|name=搜索条件',
				sortType: 'string|name=搜索类型',
				sortVal: 'name=搜索类型值',
				status: 'int|name=状态',
				activeStatus: 'int|name=激活状态',
				cate: 'int|name=分类',
				page: 'must|int|default=1',
				size: 'int',
			};

			// 取得数据
			let input = this.validateData(rules);

			let service = new ResearchService();
			let result = await service.getResearchList(input);

			// 数据格式化
			let list = result.list || [];
			for (let k = 0; k < list.length; k++) {
				if (list[k].RA_CREATE_TIME) {
					list[k].ADD_TIME = timeHelper.fmtDateCN(list[k].RA_CREATE_TIME * 1000);
				} else if (list[k].ADD_TIME) {
					list[k].ADD_TIME = timeHelper.fmtDateCN(list[k].ADD_TIME);
				}
			}

			return result;
		} catch (err) {
			console.error('获取项目列表失败', err);
			return {
				list: [],
				total: 0
			};
		}
	}

	/**
	 * 项目详情页 [接口: research_project/detail]
	 */
	async getProjectDetail() {
		// 数据校验
		let rules = {
			id: 'must|id',
		};

		// 取得数据
		let input = this.validateData(rules);

		let service = new ResearchService();
		let project = await service.getResearchDetail(input.id);
		
		// 修复：当项目不存在时，返回规范的错误信息，而不是返回null
		if (!project) {
			return { 
				code: -1, 
				message: '项目不存在或已被删除'
			};
		}
		
		if (project) {
			project.ADD_TIME = timeHelper.fmtDateCN(project.ADD_TIME);
			
			// 格式化开始和结束时间
			if (project.PROJECT_START_DATE)
				project.PROJECT_START_DATE = timeHelper.fmtDateCN(project.PROJECT_START_DATE);
				
			if (project.PROJECT_END_DATE)
				project.PROJECT_END_DATE = timeHelper.fmtDateCN(project.PROJECT_END_DATE);
		}

		return project;
	}

	/**
	 * 添加项目 [接口: research_project/insert]
	 */
	async insertProject() {
		// 数据校验
		let rules = {
			PROJECT_TITLE: 'must|string|min:2|max:50|name=项目标题',
			PROJECT_CODE: 'must|string|min:2|max:20|name=项目编号',
			PROJECT_CATE: 'must|int|name=项目分类',
			PROJECT_STATUS: 'must|int|name=项目状态',
			PROJECT_ACTIVE_STATUS: 'must|int|name=激活状态',
			PROJECT_MANAGER_ID: 'must|string|name=项目经理ID',
			PROJECT_MANAGER_NAME: 'must|string|name=项目经理',
			PROJECT_DESC: 'string|max:500|name=项目描述',
			PROJECT_GOALS: 'string|max:500|name=项目目标',
			PROJECT_START_DATE: 'date|name=开始时间',
			PROJECT_END_DATE: 'date|name=结束时间',
			PROJECT_PROGRESS: 'int|min:0|max:100|name=项目进度',
			PROJECT_PROGRESS_DESC: 'string|max:200|name=进度描述',
		};

		// 取得数据
		let input = this.validateData(rules);

		let service = new ResearchService();
		let result = await service.insertResearch(input);

		return result;
	}

	/**
	 * 编辑项目 [接口: research_project/edit]
	 */
	async editProject() {
		// 数据校验
		let rules = {
			id: 'must|id',
			PROJECT_TITLE: 'must|string|min:2|max:50|name=项目标题',
			PROJECT_CODE: 'must|string|min:2|max:20|name=项目编号',
			PROJECT_CATE: 'must|int|name=项目分类',
			PROJECT_STATUS: 'must|int|name=项目状态',
			PROJECT_ACTIVE_STATUS: 'must|int|name=激活状态',
			PROJECT_MANAGER_ID: 'must|string|name=项目经理ID',
			PROJECT_MANAGER_NAME: 'must|string|name=项目经理',
			PROJECT_DESC: 'string|max:500|name=项目描述',
			PROJECT_GOALS: 'string|max:500|name=项目目标',
			PROJECT_START_DATE: 'date|name=开始时间',
			PROJECT_END_DATE: 'date|name=结束时间',
			PROJECT_PROGRESS: 'int|min:0|max:100|name=项目进度',
			PROJECT_PROGRESS_DESC: 'string|max:200|name=进度描述',
		};

		// 取得数据
		let input = this.validateData(rules);

		let service = new ResearchService();
		let result = await service.editResearch(input);

		return result;
	}

	/**
	 * 删除项目 [接口: research_project/delete]
	 */
	async deleteProject() {
		// 数据校验
		let rules = {
			id: 'must|id',
		};

		// 取得数据
		let input = this.validateData(rules);

		let service = new ResearchService();
		let result = await service.deleteResearch(input.id);

		return result;
	}

	/**
	 * 获取项目分支列表 [接口: research_project/branch_list]
	 */
	async getBranchList() {
		// 数据校验
		let rules = {
			parentId: 'must|id',
		};

		// 取得数据
		let input = this.validateData(rules);

		let service = new ResearchService();
		let result = await service.getResearchBranchList(input.parentId);

		return result;
	}

	/**
	 * 获取父项目 [接口: research_project/parent]
	 */
	async getParent() {
		// 数据校验
		let rules = {
			id: 'must|id',
		};

		// 取得数据
		let input = this.validateData(rules);

		let service = new ResearchService();
		let result = await service.getResearchParent(input.id);

		return result;
	}

	/**
	 * 获取资源列表 [接口: research_project/resource_list]
	 */
	async getResourceList() {
		// 数据校验
		let rules = {
			projectId: 'must|id',
			page: 'int|default=1',
			size: 'int',
		};

		// 取得数据
		let input = this.validateData(rules);

		let service = new ResearchService();
		let result = await service.getResearchResourceList(input.projectId, input.page, input.size);

		// 数据格式化
		let list = result.list;
		for (let k = 0; k < list.length; k++) {
			list[k].ADD_TIME = timeHelper.fmtDateCN(list[k].ADD_TIME);
		}

		return result;
	}

	/**
	 * 创建分支 [接口: research_project/create_branch]
	 */
	async createBranch() {
		// 数据校验
		let rules = {
			parentId: 'must|id|name=父项目ID',
			name: 'must|string|min:2|max:50|name=分支名称',
			desc: 'string|max:200|name=分支描述',
		};

		// 取得数据
		let input = this.validateData(rules);

		let service = new ResearchService();
		let result = await service.createResearchBranch(input.parentId, input.name, input.desc);

		return result;
	}

	/**
	 * 合并分支 [接口: research_project/merge_branch]
	 */
	async mergeBranch() {
		// 数据校验
		let rules = {
			sourceId: 'must|id|name=源分支ID',
			targetId: 'must|id|name=目标分支ID',
		};

		// 取得数据
		let input = this.validateData(rules);

		let service = new ResearchService();
		let result = await service.mergeResearchBranch(input.sourceId, input.targetId);

		return result;
	}

	/**
	 * 更新分支 [接口: research_project/update_branch]
	 */
	async updateBranch() {
		// 数据校验
		let rules = {
			id: 'must|id',
			name: 'must|string|min:2|max:50|name=分支名称',
			desc: 'string|max:200|name=分支描述',
		};

		// 取得数据
		let input = this.validateData(rules);

		let service = new ResearchService();
		let result = await service.updateResearchBranch(input.id, input.name, input.desc);

		return result;
	}

	/**
	 * 删除分支 [接口: research_project/delete_branch]
	 */
	async deleteBranch() {
		// 数据校验
		let rules = {
			id: 'must|id',
		};

		// 取得数据
		let input = this.validateData(rules);

		let service = new ResearchService();
		let result = await service.deleteResearchBranch(input.id);

		return result;
	}

	/**
	 * 获取流程历史 [接口: research_project/flow_history]
	 */
	async getFlowHistory() {
		// 数据校验
		let rules = {
			projectId: 'must|id',
			page: 'int|default=1',
			size: 'int',
		};

		// 取得数据
		let input = this.validateData(rules);

		let service = new ResearchService();
		let result = await service.getResearchFlowHistory(input.projectId, input.page, input.size);

		// 数据格式化
		let list = result.list || [];
		for (let k = 0; k < list.length; k++) {
			list[k].ADD_TIME = timeHelper.fmtDateCN(list[k].ADD_TIME);
		}

		return result;
	}

	/** 权限检查 */
	static async _checkPermission(role, options) {
		return ResearchStateBiz.hasPermission(role, options);
	}
}

module.exports = ResearchProjectController; 